package com.p2p.backend.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.p2p.backend.controller.form.RoleForm;
import com.p2p.backend.service.OplogService;
import com.p2p.backend.service.PrivilegeService;
import com.p2p.backend.service.RoleService;
import com.p2p.base.annotation.PermissionNeed;
import com.p2p.base.model.Action;
import com.p2p.base.model.Module;
import com.p2p.base.model.Oplog;
import com.p2p.base.web.controller.BaseController;

/**
 * leiwen@yiqihao.com
 */
@Controller
@RequestMapping("role")
public class RoleController extends BaseController {

	@Resource
	private RoleService roleService;

	@Resource
	private PrivilegeService privilegeService;

	@PermissionNeed("privilege_role_list")
	@RequestMapping("list")
	public void list(Model model) {
		model.addAttribute("res", 1);
		Map<String, Object> data = new HashMap<String, Object>();
		List<Map<String, Object>> list = roleService.listAll();
		data.put("rows", list);
		data.put("total", list.size());
		model.addAttribute("data", data);
	}

	@PermissionNeed("privilege_role_management")
	@RequestMapping("add")
	public String add(Model model) {
		List<Map<String, Object>> privilegeList = privilegeService.listAll();
		model.addAttribute("privilegeList", privilegeList);
		return "role/add";
	}

	@PermissionNeed("privilege_role_management")
	@RequestMapping("edit")
	public String edit(@RequestParam Integer id, Model model) {
		Map<String, Object> role = roleService.getDetail(id);
		model.addAttribute("role", role);
		// TODO 优化，转换成key-map
		List<Map<String, Object>> ownPrivilegeList = privilegeService
				.listByRole(id);
		List<Map<String, Object>> privilegeList = privilegeService.listAll();
		for (Map<String, Object> p : privilegeList) {
			boolean contains = contains(ownPrivilegeList, p);
			if (contains) {
				p.put("checked", "checked");
			}
		}
		model.addAttribute("privilegeList", privilegeList);

		return "role/edit";
	}

	private boolean contains(List<Map<String, Object>> ownPrivilegeList,
			Map<String, Object> p) {
		for (Map<String, Object> own : ownPrivilegeList) {
			if (own.get("acl").equals(p.get("acl"))) {
				return true;
			}
		}
		return false;
	}

	@PermissionNeed("privilege_role_management")
	@RequestMapping("doadd")
	public void doadd(RoleForm params, Model model, HttpServletRequest request) {
		params.strToArray();
		roleService.add(params);
		model.addAttribute("res", 1);
		Oplog oplog = super.oplog(request);
		oplog.setModule(Module.PRIVILEGE_ROLE);
		oplog.setAction(Action.ADD);
		oplog.setParams(ToStringBuilder.reflectionToString(params));
		OplogService.log(oplog);
	}

	@PermissionNeed("privilege_role_management")
	@RequestMapping("doupdate")
	public void doupdate(RoleForm params, Model model,
			HttpServletRequest request) {
		params.strToArray();
		roleService.update(params);
		model.addAttribute("res", 1);
		Oplog oplog = super.oplog(request);
		oplog.setModule(Module.PRIVILEGE_ROLE);
		oplog.setAction(Action.UPDATE);
		oplog.setParams(ToStringBuilder.reflectionToString(params));
		OplogService.log(oplog);
	}

	@PermissionNeed("privilege_role_management")
	@RequestMapping("delete")
	public void delete(Integer id, Model model, HttpServletRequest request) {
		boolean succeed = roleService.delete(id);
		Oplog oplog = super.oplog(request);
		oplog.setModule(Module.PRIVILEGE_ROLE);
		oplog.setAction(Action.DELETE);
		oplog.setParams("id=" + id);

		if (succeed) {
			model.addAttribute("res", 1);
			oplog.setRemark("ok");
		} else {
			model.addAttribute("res", 0);
			model.addAttribute("msg", "该角色下存在用户");
			oplog.setRemark("failed");
		}
		OplogService.log(oplog);
	}
}
